﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class Finance_Report : System.Web.UI.Page
{
    private int _totalBillsCount;
    private decimal _totalIncome;
    private decimal _totalRealIncome;
    private decimal _totalPayout;
    private decimal _totalRealPayout;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindYearsDll();
            ddlYear.SelectedValue = DateTime.Now.Year.ToString();
            BindList();
        }
    }

    //绑定4个季度
    private void BindList()
    {
        dlQuartersList.DataSource = new int[4] { 1, 2, 3, 4 };
        dlQuartersList.DataBind();
    }

    protected void BindYearsDll()
    {
        //绑定年
        for (int i = 2007; i <= DateTime.Now.Year; i++)
        {
            ListItem li = new ListItem(i.ToString(), i.ToString());
            ddlYear.Items.Add(li);
        }
    }

    protected CrossTabReportCollection GetQuarterDetails(int quarter)
    {
        return FinanceManager.GetReportByYear(quarter, Convert.ToInt32(ddlYear.SelectedValue));
    }

 
    protected void gvMonthOfBills_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int billsCount = Convert.ToInt32(e.Row.Cells[1].Text);
            _totalBillsCount += billsCount;
            e.Row.Cells[1].Text = billsCount.ToString();

            decimal income = Convert.ToDecimal(e.Row.Cells[2].Text);
            _totalIncome += income;
            e.Row.Cells[2].Text = Formater.FormatMoney(income);

            decimal realIncome = Convert.ToDecimal(e.Row.Cells[3].Text);
            _totalRealIncome += realIncome;
            e.Row.Cells[3].Text = Formater.FormatMoney(realIncome);

            decimal payout = Convert.ToDecimal(e.Row.Cells[4].Text);
            _totalPayout += payout;
            e.Row.Cells[4].Text = Formater.FormatMoney(payout);

            decimal realPayout = Convert.ToDecimal(e.Row.Cells[5].Text);
            _totalRealPayout += realPayout;
            e.Row.Cells[5].Text = Formater.FormatMoney(realPayout);

            e.Row.Cells[6].Text = Formater.FormatMoney(Convert.ToDecimal(e.Row.Cells[6].Text));
            e.Row.Cells[7].Text = Formater.FormatMoney(Convert.ToDecimal(e.Row.Cells[7].Text));
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[1].Text = _totalBillsCount.ToString();
            e.Row.Cells[2].Text =Formater.FormatMoney(_totalIncome);
            e.Row.Cells[3].Text = Formater.FormatMoney(_totalRealIncome);
            e.Row.Cells[4].Text = Formater.FormatMoney(_totalPayout);
            e.Row.Cells[5].Text = Formater.FormatMoney(_totalRealPayout);
            e.Row.Cells[6].Text = Formater.FormatMoney(_totalIncome - _totalPayout);
            e.Row.Cells[7].Text = Formater.FormatMoney(_totalRealIncome - _totalRealPayout);
            //清零，下一个GridView重新计算
            _totalBillsCount = 0;
            _totalIncome = 0;
            _totalPayout = 0;
            _totalRealIncome = 0;
            _totalRealPayout = 0;
        }
    }
    protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindList();
    }
}
